﻿Imports DevExpress.XtraEditors

Public Class frmNghiemThu_TrangThai
    Dim Me_AddEdit As String
    Dim Me_Dtb1, Me_Dtb2 As New DataTable

#Region "proc"

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "chon"
            .Columns(i).Caption = " "
            .Columns(i).Width = 20

            i = 1
            .Columns(i).FieldName = "sophieu"
            .Columns(i).Caption = "Số phiếu"
            .Columns(i).Width = 40
            .Columns(i).OptionsColumn.AllowEdit = False

            i = 2
            .Columns(i).FieldName = "so_don"
            .Columns(i).Caption = "Số đơn"
            .Columns(i).Width = 50
            .Columns(i).OptionsColumn.AllowEdit = False

            i = 3
            .Columns(i).FieldName = "khachhang"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 80
            .Columns(i).OptionsColumn.AllowEdit = False

        End With

        With Me.GridView2
            i = 0
            .Columns(i).FieldName = "chon"
            .Columns(i).Caption = " "
            .Columns(i).Width = 20

            i = 1
            .Columns(i).FieldName = "sophieu"
            .Columns(i).Caption = "Số phiếu"
            .Columns(i).Width = 40
            .Columns(i).OptionsColumn.AllowEdit = False

            i = 2
            .Columns(i).FieldName = "so_don"
            .Columns(i).Caption = "Số đơn"
            .Columns(i).Width = 50
            .Columns(i).OptionsColumn.AllowEdit = False

            i = 3
            .Columns(i).FieldName = "khachhang"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 80
            .Columns(i).OptionsColumn.AllowEdit = False

        End With


    End Sub

    Private Sub LoadGrid()
        Dim myNghiemThuF As New NghiemThuF

        Me_Dtb1 = myNghiemThuF.GetListChietTinh(Me.txtSoTc.EditValue, 0)
        Me.GridControl1.DataSource = Me_Dtb1

        Me_Dtb2 = myNghiemThuF.GetListChietTinh(Me.txtSoTc.EditValue, Me.txtTrangThai.EditValue)
        Me.GridControl2.DataSource = Me_Dtb2

    End Sub

#End Region

    Private Sub frmChietTinh_CapNhat_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.GridView1.Focus()
    End Sub

    Private Sub frmNghiemThu_TrangThai_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitGrid()
        LoadGrid()

        If Me.txtTrangThai.EditValue = 2 Then
            Me.lblGrid2.Text = "Danh sách chiết tính chuyển sang kỳ sau:"
        ElseIf Me.txtTrangThai.EditValue = 3 Then
            Me.lblGrid2.Text = "Danh sách chiết tính hoãn:"
        End If
    End Sub

    Private Sub btnR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnR.Click
        Dim myNghiemThuF As New NghiemThuF
        Dim i As Integer
        i = 0
        Do While i <= Me.GridView1.DataRowCount - 1
            If Me.GridView1.GetRowCellValue(i, "chon") = True Then
                If Not myNghiemThuF.UpdateTrangThai(Me.txtSoTc.EditValue, Me.GridView1.GetRowCellValue(i, "sophieu"), Me.txtTrangThai.EditValue) Then
                    Exit Do
                End If
                Me_Dtb2.Rows.Add()
                Me_Dtb2.Rows(Me_Dtb2.Rows.Count - 1).Item("chon") = False
                Me_Dtb2.Rows(Me_Dtb2.Rows.Count - 1).Item("sophieu") = Me.GridView1.GetRowCellValue(i, "sophieu")
                Me_Dtb2.Rows(Me_Dtb2.Rows.Count - 1).Item("so_don") = Me.GridView1.GetRowCellValue(i, "so_don")
                Me_Dtb2.Rows(Me_Dtb2.Rows.Count - 1).Item("khachhang") = Me.GridView1.GetRowCellValue(i, "khachhang")

                Me.GridView1.DeleteRow(i)
            Else
                i = i + 1
            End If
        Loop
    End Sub

    Private Sub btnL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnL.Click
        Dim myNghiemThuF As New NghiemThuF
        Dim i As Integer
        i = 0
        Do While i <= Me.GridView2.DataRowCount - 1
            If Me.GridView2.GetRowCellValue(i, "chon") = True Then
                If Not myNghiemThuF.UpdateTrangThai(Me.txtSoTc.EditValue, Me.GridView2.GetRowCellValue(i, "sophieu"), 0) Then
                    Exit Do
                End If
                Me_Dtb1.Rows.Add()
                Me_Dtb1.Rows(Me_Dtb1.Rows.Count - 1).Item("chon") = False
                Me_Dtb1.Rows(Me_Dtb1.Rows.Count - 1).Item("sophieu") = Me.GridView2.GetRowCellValue(i, "sophieu")
                Me_Dtb1.Rows(Me_Dtb1.Rows.Count - 1).Item("so_don") = Me.GridView2.GetRowCellValue(i, "so_don")
                Me_Dtb1.Rows(Me_Dtb1.Rows.Count - 1).Item("khachhang") = Me.GridView2.GetRowCellValue(i, "khachhang")

                Me.GridView2.DeleteRow(i)
            Else
                i = i + 1
            End If
        Loop
    End Sub


End Class